package com.zipking.cloud.springbootmybatis.util;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.formula.functions.T;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class PagerUtilsExt<T> {

    private List<T> data;

    private int pageSize;



    /**

     * @param data     原始数据

     * @param pageSize 每页条数

     */




    /**

     * 获取某页数据，从第1页开始

     *

     * @param pageNum 第几页

     * @return 分页数据

     */

    public List<T> page(int pageNum) {

        if (pageNum < 1) {

            pageNum = 1;

        }

        int from = (pageNum - 1) * pageSize;

        int to = Math.min(pageNum * pageSize, data.size());

        if (from > to) {

            from = to;

        }

        return data.subList(from, to);

    }



    /**

     * 获取总页数

     */

    public int getPageCount() {

        if (pageSize == 0) {

            return 0;

        }

        return data.size() % pageSize == 0 ? (data.size() / pageSize) : (data.size() / pageSize + 1);

    }



    /**

     * 元素迭代器

     */

    public Iterator<List<T>> iterator() {

        return new Itr();

    }



    private class Itr implements Iterator<List<T>> {

        int page = 1;



        Itr() {

        }



        @Override

        public boolean hasNext() {

            return page <= getPageCount();

        }



        @Override
        public List<T> next() {

            int i = page;

            if (i > getPageCount())

                return new ArrayList<>();



            page = i + 1;

            return PagerUtilsExt.this.page(i);

        }

    }



    public static void main(String[] args) {



    }


    /**
     * 直接获取分页列表List<T>
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @param list 待分页集合
     * @param <T> T
     * @return 分页查询后list
     */
    public static <T> List<T> getList(int pageNum, int pageSize, List<T> list){
        return getPageInfo(pageNum, pageSize, list).getList();
    }

    /**
     * 获取分页结果PageInfo<T>
     * @param currentPage 当前页
     * @param pageSize 每页大小
     * @param list  待分页集合
     * @param <T> T
     * @return 分页信息
     */
    public static <T> PageInfo<T> getPageInfo(int currentPage, int pageSize, List<T> list) {
        int total = list.size();
        if (total > pageSize) {
            int toIndex = pageSize * currentPage;
            if (toIndex > total) {
                toIndex = total;
            }
            if(pageSize * (currentPage - 1)<toIndex){
                list = list.subList(pageSize * (currentPage - 1), toIndex);
            }else{
                list.clear();
            }
        }else{
            Page<T> page = new Page<>(currentPage, pageSize);
            list.clear();
            page.addAll(list);
            page.setPages((total + pageSize - 1) / pageSize);
            page.setTotal(total);
            return new PageInfo<>(page);
        }
        Page<T> page = new Page<>(currentPage, pageSize);
        page.addAll(list);
        page.setPages((total + pageSize - 1) / pageSize);
        page.setTotal(total);
        return new PageInfo<>(page);
    }

}
